const service = require("./winRecordService.js");
const utils = require("../../../utils/util");
const app = getApp();
var noPage = 1; // 未领取页码初始值
var awardPage = 1; //实物页码初始值
Page({
  data: {
    prize_data: [],
    tabs: ["未领取", "实物奖品"],
    activeIndex: 0,   // tab切换标记
    hasNextPage_no: true, // 未领取列表是否有下一页
    hasNextPage_award: true, // 实物奖品列表是否有下一页
    noReseiveData: [],  // 未领取列表
    awardListData: []   // 实物奖品列表
  },
  onLoad: function() {
    noPage = 1;
    awardPage = 1;
    app.loginPromise.then(() => {
      this.requestNoReseiveData();
      // this.requestawardListData();
    });
  },
  // 未领取请求
  requestNoReseiveData: function() {
    service.requestNoReceive(noPage, data => {
      wx.hideLoading();
      // 数据格式处理
      if(data.execData.user_winning_collects.length <= 0){
        this.setData({hasNextPage_no: false})
      }
      let dataArr = data.execData.user_winning_collects.filter(item => {
        // 过期false, 未过期为true
        +new Date() <= +new Date(item.deadline)
          ? (item.isOverdue = true)
          : (item.isOverdue = false);
        item.createdTime = utils.formatDateDot(item.created_at);
        // 领取时限处理
        item.toggleTime = this.toggleTime(
          +new Date(item.deadline) - +new Date()
        );
        return item;
      });

      let arr = noPage == 1 ? dataArr : this.data.noReseiveData.concat(dataArr);
      this.setData({ noReseiveData: arr }); 
    });
  },
  // 实物奖品请求
  requestAwardListData: function() {
    service.requestAwardList(awardPage, data => {
      // 数据格式处理
      let dataArr = data.execDatas.filter(item => {
        item.createdTime = utils.formatDateDot(item.createTime);
        return item;
      });
      wx.hideLoading();
      let arr =
        awardPage == 1 ? dataArr : this.data.awardListData.concat(dataArr);
      this.setData({ awardListData: arr });
    });
  },
  /**
   * 果冻领取
   */
  operationAction: function(e){
    // 领取果冻接口成功后播放瓶子动画
    let id = e.currentTarget.dataset.id
    service.requestJelly(id, data => { 

      // 领取成功后同步页面状态
      let dataArr = this.data.noReseiveData.filter(item => {
        if(item.id == id){
          item.is_share = true;
        }
        return item;
      })
      this.setData({ noReseiveData: dataArr }); 
      

      let itemData = e.currentTarget.dataset.item.filter(item => {
        // 过滤掉字符串中得非数字字符
        var str = item.prize_name
        item.prize_name = str.replace(/[^0-9]/ig,"");
        return item
      })
      this.setData({ prize_data: itemData });
      var sweetsAnimation = this.selectComponent("#sweetsAnimation");
      sweetsAnimation.show()
    })

  },
  toggleTime: function(time) {
    time = Math.floor(time / 1000);
    let h = Math.floor(time / 60 / 60),
      m = Math.floor((time - h * 60 * 60) / 60),
      s = time % 60;
    if (h > 0) {
      return h + "h " + m + "min";
    } else {
      return m + "min";
    }
  },
  tabClick(e) {
    this.setData({
      activeIndex: e.currentTarget.id
    });
  },
  // 下拉刷新
  onPullDownRefresh: function() {
    console.log("下拉刷新");
    wx.stopPullDownRefresh();
    if (this.data.activeIndex == 0) {
      noPage = 1;
      this.requestNoReseiveData();
    } else {
      awardPage = 1;
      this.requestAwardListData();
    }
  },
  // 加载更多
  onReachBottom: function() {
    console.log("加载更多");
    if (this.data.activeIndex == 0) {
      if(this.data.hasNextPage_no){
        ++noPage;
        this.requestNoReseiveData();
      }
    } else {
      if(this.data.hasNextPage_award){
        ++awardPage;
        this.requestAwardListData();
      }
    }
  }
});
